﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using ERP.Domain.SalesManagement;
using Sell.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace Sell.Services.Verifications
{
    public class VerificationServices : IVerificationServices
    {
       
        private readonly IVerificationRepository _verificationRepository;
        public VerificationServices(IVerificationRepository verificationRepository)
        {
            
            _verificationRepository = verificationRepository;
        }
        /// <summary>
        /// 添加采购核价
        /// </summary>
        /// <param name="verifications"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddVerification(Verification verifications)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "采购核价添加成功";
            var list = await _verificationRepository.AddAsync(verifications);
            res.Data = list;
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 采购核价删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteVerification(int id)
        {
            ApiResult<int> res = new ApiResult<int>();
            try
            {
                //根据ID获取客户信息
                var Ver = await _verificationRepository.GetModelAsync(id);
                if (Ver == null)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "采购核价不存在";
                    return res;
                }
                //将IsDelete状态设置为true
                Ver.IsDelete = true;
                //更新客户信息
                var result = await _verificationRepository.Update(Ver);
                res.Code = EnumCode.Ok;
                res.Message = "采购核价删除成功";
                res.Data = result;

            }
            catch (Exception ex)
            {
                res.Code = EnumCode.Fail;
                res.Message = $"删除采购核价失败:{ex.Message}";
            }
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 采购核价修改
        /// </summary>
        /// <param name="verifications"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateVerification(Verification verifications)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "采购核价修改成功";
            var list = await _verificationRepository.Update(verifications);
            res.Data = list;
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 修改审核状态
        /// </summary>
        /// <param name="id">核价单ID</param>
        /// <param name="reviewStatus">审核状态 (0-未审核, 1-已审核)</param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateReviewStatus(int id, int reviewStatus)
        {
            ApiResult<int> res = new ApiResult<int>();
            try
            {
                // 根据ID获取核价单信息
                var verification = await _verificationRepository.GetModelAsync(id);
                if (verification == null)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "采购核价不存在";
                    return res;
                }

                // 验证审核状态值是否有效
                if (reviewStatus < 0 || reviewStatus > 1)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "审核状态值无效，有效值：0-未审核, 1-已审核";
                    return res;
                }

                // 更新审核状态
                verification.Review = reviewStatus;
                var result = await _verificationRepository.Update(verification);
                
                res.Code = EnumCode.Ok;
                res.Message = reviewStatus == 0 ? "未审核" : "已审核";
                res.Data = result;
            }
            catch (Exception ex)
            {
                res.Code = EnumCode.Fail;
                res.Message = $"修改审核状态失败: {ex.Message}";
            }
            return await Task.FromResult(res);
        }
    }
}
